import '../../public.dart';
import '../home/view.dart';
import '../mine/view.dart';
import '../wallet/view.dart';
import '../history/view.dart';
import 'bottom.dart';

List<RootTabPage> pages = <RootTabPage>[
  RootTabPage(
    icon: const Icon(Icons.home),
    activedIcon: const Icon(Icons.home, color: Colors.lightBlue),
    name: 'Home',
    activedColor: Colors.lightBlue,
    page: const HomeScreen(),
  ),
  RootTabPage(
    icon: const Icon(Icons.wallet),
    activedIcon: const Icon(Icons.wallet, color: Colors.lightBlue),
    name: 'Wallet',
    activedColor: Colors.lightBlue,
    page: const WalletPage(),
  ),
  RootTabPage(
    icon: const Icon(Icons.history),
    activedIcon: const Icon(Icons.history, color: Colors.lightBlue),
    name: 'History',
    activedColor: Colors.lightBlue,
    page: const HistoryPage(),
  ),
  RootTabPage(
    icon: const Icon(Icons.person),
    activedIcon: const Icon(Icons.person, color: Colors.lightBlue),
    name: 'Profile',
    activedColor: Colors.lightBlue,
    page: const MinePage(),
  ),
];

class TabNavigator extends StatefulWidget {
  final int? defaultActive;
  const TabNavigator(this.defaultActive, {super.key});
  @override
  State<TabNavigator> createState() => _TabNavigatorState();
}

class _TabNavigatorState extends State<TabNavigator> {
  late PageController _pageController;
  late int _selectedIndex;

  int _lastClickTime = 0;

  Future<bool> _doubleExit() {
    int nowTime = DateTime.now().microsecondsSinceEpoch;
    if (_lastClickTime != 0 && nowTime - _lastClickTime > 1500) {
      return Future.value(true);
    } else {
      _lastClickTime = DateTime.now().microsecondsSinceEpoch;
      Future.delayed(const Duration(milliseconds: 1500), () {
        _lastClickTime = 0;
      });
      Toast.show('doubble click to exit');
      return Future.value(false);
    }
  }

  @override
  void initState() {
    super.initState();
    if (mounted) {
      if (widget.defaultActive != null) {
        _selectedIndex = widget.defaultActive ?? 0;
        _pageController = PageController(
          initialPage: widget.defaultActive ?? 0,
        );
      } else {
        _selectedIndex = 0;
        _pageController = PageController();

        /// 检查app 版本
        // AppVersion.checkVersion(context);
      }
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PopScope(
        onPopInvokedWithResult: (didPop, result) async {
          if (!didPop) {
            final shouldExit = await _doubleExit();
            if (shouldExit) {
              if (mounted) {
                if (mounted) {
                  Navigator.of(context).pop();
                }
              }
            }
          }
        },
        child: PageView.builder(
          itemBuilder: (ctx, index) => pages[index].page,
          itemCount: pages.length,
          controller: _pageController,
          physics: const NeverScrollableScrollPhysics(),
          onPageChanged: (index) {
            setState(() {
              if (mounted) {
                _selectedIndex = index;
              }
            });
          },
        ),
      ),
      bottomNavigationBar: BottomBar(
        pages,
        currentIndex: _selectedIndex,
        onSelect: (index) {
          _pageController.jumpToPage(index);
        },
      ),
    );
  }
}
